﻿@{
    Layout = "~/Areas/Admin/Views/Shared/_AdminMainLayout.cshtml";
}
<br />

<script language="javascript" type="text/javascript">
    function ReloadTree(reloadRightSide) {
        try {
            $.ajax({
                url: '@Url.Action("Index", "SiteTree")' + '?time=' + new Date().getTime(),
                type: 'POST',
                data: '',
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    $(".leftSide").html(data);
                    if (reloadRightSide)
                        $(".rightSide").html('');
                },
                error: function () {
                    $(".leftSide").html('An error occured, please refresh page and try again.');
                }
            });
        } catch (e) {
            alert("Unexpected Error (CallTree) : " + e);
        }
    }
</script>

<div class="leftSide">
    @Html.Action("Index", "SiteTree")
</div>
<div class="rightSide" id="adminRightSide">
    @RenderBody()
</div>
@section scripts{
    <script src="@Url.Content("~/Areas/Admin/Scripts/jquery.contextmenu.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $().ready(function () {

            /* site menu tree */
            var SiteOption = {
                width: 150, items: [
                            @{
                                if (ViewContext.ParentActionViewContext != null && ViewContext.ParentActionViewContext.RouteData.Values["controller"] == "CountrySiteScript")
                                {
                                    <text>{ text: "Add general script", icon: "/Areas/Admin/Content/themes/admin/img/contextmenu/icons/wi0126-16.gif", alias: "Site-NewScript", action: menuAction, style: "cursor:hand; cursor:pointer" },</text>
                                }
                                else
                                {
                                    <text>{ text: "Add new page", icon: "/Areas/Admin/Content/themes/admin/img/contextmenu/icons/wi0126-16.gif", alias: "Site-NewPage", action: menuAction, style: "cursor:hand; cursor:pointer" },</text>
                                }
                            }
                ], onShow: applyrule, onContextMenu: BeforeContextMenu
            };

            /* page menu tree */
            var PageOption = {
                width: 150, items: [
                            @{
                                if (ViewContext.ParentActionViewContext != null && ViewContext.ParentActionViewContext.RouteData.Values["controller"] == "CountrySiteScript")
                                {
                                    <text>
                                    { text: "Add new script", icon: "/Areas/Admin/Content/themes/admin/img/contextmenu/icons/wi0126-16.gif", alias: "Page-NewScript", action: menuAction },
                                   </text>
                                }
                                else
                                {
                                    //only admins can add/copy page
                                    //if (CenturyAuthorization.Authorize(HttpContext.Current.User.Identity.Name, new ActionModule { Name = "Administration", DisplayName = "Administration" }))
                                    //{
                                        <text>
                                            { text: "New sub page", icon: "/Areas/Admin/Content/themes/admin/img/contextmenu/icons/wi0126-16.gif", alias: "Page-NewSubPage", action: menuAction },
                                            { text: "Copy as...", icon: "/Areas/Admin/Content/themes/admin/img/contextmenu/icons/ei0021-16.gif", alias: "Page-CopyAs", action: menuAction },
                                        </text>
                                    //}

                                    <text>
                                    { text: "Disable/Enable", icon: "/Areas/Admin/Content/themes/admin/img/contextmenu/icons/ac0036-16.gif", alias: "Page-Disable", action: menuAction },
                                    { text: "Delete", icon: "/Areas/Admin/Content/themes/admin/img/contextmenu/icons/cross.png", alias: "Page-Delete", action: menuAction },
                                    </text>
                                }
                            }
                ], onShow: applyrule, onContextMenu: BeforeContextMenu
            };

            var selectedNodeId = -1;

            function showForm(urlAction, _data, _methodeType) {
                try {
                    var _URL = urlAction + '/' + _data;
                    $.ajax({
                        url: _URL,
                        type: _methodeType != null ? _methodeType : 'POST',
                        data: '',
                        contentType: 'application/json; charset=utf-8',
                        beforeSend: function () {
                            $('#adminRightSide').css('background', 'url(/Areas/Admin/Content/themes/admin/img/gifs/loading.gif) no-repeat');
                        },
                        complete: function () {
                            $('#adminRightSide').css('background', 'none');
                            ReloadTree();
                        },
                        success: function (data) {
                            $('#adminRightSide').html(data);
                        },
                        error: function () {
                            $("#adminRightSide").html('error');
                        }
                    });
                } catch (e) {
                    alert(e);
                }
            }

            function menuAction() {
                var event = this.data.alias;
                var elementID = $("#selectedContextMenuId").val().replace('CS-', '').replace('P-', '');
                switch (event) {
                    /* page issues */
                    case 'Page-NewSubPage':
                        document.location.href = '@Url.Action("Add", "PageManagement")' + '/' + elementID;
                    break;

                case 'Page-CopyAs':
                    document.location.href = '@Url.Action("CopyAs", "PageManagement")' + '/' + elementID;
                           break;

                       case 'Page-Disable':
                           document.location.href = '@Url.Action("ChangeStatus", "PageManagement")' + '/' + elementID;
                    break;

                case 'Page-Delete':
                    document.location.href = '@Url.Action("Delete", "PageManagement")' + '/' + elementID;
                    break;

                case 'Page-NewScript':
                    document.location.href = '@Url.Action("Add", "CountrySiteScript")' + '/' + elementID;
                    break;



                    /* site issues */
                case 'Site-NewPage':
                    document.location.href = '@Url.Action("Add", "PageManagement")';
                    break;

                case 'Site-NewScript':
                    showForm('@Url.Action("Add", "CountrySiteScript")', null);
                    break;

                default:
                    break;
            }
        }

            function applyrule(menu) {
                // code here
            }

            function BeforeContextMenu() {
                $("#selectedContextMenuId").val($(this).find('.t-input').val());
                return true;
            }

            $(".SP-ITEM").contextmenu(PageOption, 'pageOptions');
            $(".CS-ITEM").contextmenu(SiteOption, 'siteOptions');

            // decision of selected item
        @if (ViewContext.ParentActionViewContext != null && ViewContext.ParentActionViewContext.RouteData.Values["id"] == null)
        {
            <text>$('input[value=CS-@Century.Web.UI.MVC.FrameworkToken.UserToken.CurrentSite.Id]').prev().addClass('t-state-selected');</text>
        }
        else
        {
            <text>$('input[value=P-@Session["CurrentPageId"]]').prev().addClass('t-state-selected');</text>
        }

        });


    </script>
    @RenderSection("scripts", required: false)
}